﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace XYAC.Framework.MonitorTireMaterialManual.Views
{
    /// <summary>
    /// EditableTextBlock.xaml 的交互逻辑
    /// </summary>
    public partial class EditableTextBlock : UserControl
    {
        public EditableTextBlock()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        /// <summary>
        /// 当值发生变化时触发的事件。
        /// </summary>
        public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent(
            "ValueChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<double>), typeof(EditableTextBlock));

        /// <summary>
        /// CLR 事件包装，便于在代码中订阅和触发
        /// </summary>
        public event RoutedPropertyChangedEventHandler<double> ValueChanged
        {
            add => AddHandler(ValueChangedEvent, value);
            remove => RemoveHandler(ValueChangedEvent, value);
        }

        public bool ShowButtonSpinner
        {
            get { return (bool)GetValue(ShowButtonSpinnerProperty); }
            set { SetValue(ShowButtonSpinnerProperty, value); }
        }

        public static readonly DependencyProperty ShowButtonSpinnerProperty =
            DependencyProperty.Register("ShowButtonSpinner", typeof(bool), typeof(EditableTextBlock), new PropertyMetadata(false));

        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(EditableTextBlock), new PropertyMetadata(0.0));




        public double Increment
        {
            get { return (double)GetValue(IncrementProperty); }
            set { SetValue(IncrementProperty, value); }
        }

        public static readonly DependencyProperty IncrementProperty =
            DependencyProperty.Register("Increment", typeof(double), typeof(EditableTextBlock), new PropertyMetadata(1.0));

        private void NumericUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            // 创建新的 RoutedPropertyChangedEventArgs<double> 事件参数，传入旧值和新值
            var args = new RoutedPropertyChangedEventArgs<double>(e.OldValue, e.NewValue, ValueChangedEvent);

            // 触发事件
            RaiseEvent(args);
        }

        private void NumericUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<float> e)
        {
            this.Value = Math.Round(e.NewValue, 6);
        }

        private void textBlock_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.textBlock.Visibility = Visibility.Collapsed;
            this.textBox.Visibility = Visibility.Visible;
            this.textBox.Focus();
        }



        private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.Key == Key.Escape)
            {
                if (double.TryParse(this.textBox.ToString(), out double value) == true)
                {
                    //此处退出编辑模式
                    this.textBlock.Visibility = Visibility.Visible;
                    this.textBox.Visibility = Visibility.Collapsed;
                    this.textBlock.Focus();
                }
                else
                {
                    MessageBox.Show("输入字符中存在非数字字符,请重试");
                    this.textBox.Focus();
                }

            }
        }

    }
}
